<?php

// +----------------------------------------------------------------------
// | EasyAdmin
// +----------------------------------------------------------------------
// | PHP交流群: 763822524
// +----------------------------------------------------------------------
// | 开源协议  https://mit-license.org
// +----------------------------------------------------------------------
// | github开源项目：https://github.com/zhongshaofa/EasyAdmin
// +----------------------------------------------------------------------

namespace app\common\controller;
use think\facade\Db;

/**
 * Class AdminController
 * @package app\common\controller
 */
class BasicOrderInfo
{
    public $thumb;
    public $order_type;//1-服务 2-会员充值 3-年卡
    public $type;//针对服务（$order_type = 1）1-服务2-套餐3-商品
    public $order;
    public $pay_method_name;
    public $products = [];
    public $receivable = 0;//原价
    public $title;
    public $list = [];


    public function __construct($order_type,$order){
        $this->order_type = $order_type;
        $this->order = $order;
        $this->getDataInfo();

    }
    //

    //服务列表图
    public function getDataInfo(){
       
        if($this->order_type == 1){
            foreach ($this->order as $key => $value) {
                if($value['type'] == 1){
                    $storeCategory = Db::name('store_category') -> where(['cate_id' => $value['server_id']]) -> field('image,cate_name') -> findOrEmpty();
                    if(count($storeCategory) > 0){
                        $this->thumb = $storeCategory['image'];
                        $this->title = $storeCategory['cate_name'];
                    }
                    
                    $this->receivable += $value['sum_price'];
                    $orderProducts = Db::name('orders_products') -> where(['orders_id' => $value['orders_id'],'type' => 1,'server_id' => $value['server_id']] ) -> field('attr,num,product_id') -> select() -> toArray();
                    if(count($orderProducts) > 0){
                        foreach ($orderProducts as $k => $v) {
                            $storeProduct = Db::name('store_product') -> where(['products_id' => $v['product_id']]) -> field('cover_picture,products_title') -> findOrEmpty();
                            $pro = [
                                'product_id' => $v['product_id'],
                                'thumb' => $storeProduct['cover_picture'],
                                'num' => $v['num'],
                                'price' => $value['products_price']/$v['num'],
                                'title' => $storeProduct['products_title'].' '.$v['attr'],
                            ];
                            array_push($this->products, $pro);

           
                        }
                    }
                    $this->list[$key] = [
                        'title' => $this->title,
                        'thumb' => $this->thumb,
                        'products' => $this->products,
                        'price' => $value['act_price']
                    ];
                    
                    
                }
                if($value['type'] == 2){
                    $package = Db::name('package') -> where(['id' => $this->order['id']]) -> field('thumb,name') -> findOrEmpty();
                    $this->$receivable = $value['amount'] + $value['send_amount'];
                    $this->list[$key] = [
                        'title' => $package['name'],
                        'thumb' => $package['thumb'],
                        'products' => $this->products,
                        'price' => $value['amount']
                    ];
                }
                if($value['type'] == 3){
                    $storeProduct = Db::name('store_product') -> where(['products_id' => $this->order['server_id']]) -> field('cover_picture,products_titile');
                    $this->$receivable = $value['amount'];
                    $this->list[$key] = [
                        'title' => $storeProduct['products_titile'].' '.$value['attr'],
                        'thumb' => $storeProduct['thumb'],
                        'products' => $this->products,
                        'price' => $value['amount']
                    ];
                    
                }
            }
        }
        if($this->order_type == 2){
            foreach ($this->order as $key => $value) {
                $this->receivable += $value['amount'] + $value['send_amount'];
                $this->list[$key] = [
                    'title' => "会员充值",
                    'thumb' => "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.616pic.com%2Fys_bnew_img%2F00%2F10%2F76%2FSvsngA3ipK.jpg&refer=http%3A%2F%2Fpic.616pic.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1653007769&t=5723ace6a7ef6a6d17b7136215701108",
                    'products' => $this->products,
                    'price' => $value['amount']
                ];
            }
            
        }
        if($this->order_type == 3){
            foreach ($this->order as $key => $value) {
                $this->thumb = Db::name('card_image') -> where(['card_id' => $value['card_id'],'type' => 1]) -> value('image');
                $this-> receivable = $value['amount'];
                $this->list[$key] = [
                    'title' => "体验卡",
                    'thumb' => $this->thumb,
                    'products' => $this->products,
                    'price' => $value['amount']
                ]; 
            }
            
        }
        return $this;
       
    }

    
   


}